#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import requests
from requests import session

baseUrl = ""
session = requests.session()


# login
def login(username, password):
    loginUrl = baseUrl + "/j_spring_security_check"
    body = {
        "j_username": username,
        "j_password": password
    }
    resp = session.post(loginUrl, body)
    if resp.status_code != 200:
        raise Exception("Login failed")
    return session.cookies['JSESSIONID']


pass


# appList
def app_list():
    url = baseUrl + "/jobinfo/pageList"
    body = {
        "jobGroup": "",
        "jobDesc": "",
        "executorAddress": "",
        "executorHandler": "",
        "author": "",
        "start": "0",
        "length": "1000"
    }
    resp = session.post(url, body)
    if (resp.status_code != 200):
        print(resp.status_code)
        raise Exception("App list failed")
    return resp.json().get("data")
    pass


class Job:
    def __init__(self, id, jobName, jobGroup, jobDesc, jobCron, executorParam, executorAddress, executorHandler, author,
                 jobstatus, addTime, updateTime):
        self.id = id
        self.jobName = jobName
        self.jobGroup = jobGroup
        self.jobDesc = jobDesc
        self.jobCron = jobCron
        self.executorParam = executorParam
        self.executorAddress = executorAddress
        self.executorHandler = executorHandler
        self.author = author
        self.jobstatus = jobstatus
        self.addTime = addTime
        self.updateTime = updateTime
        pass


def json_to_job(dct):
    return Job(dct['id'], dct['jobName'], dct['jobGroup'], dct['jobDesc'], dct['jobCron'], dct['executorParam'], dct['executorAddress'], dct['executorHandler'], dct['author'], dct['jobstatus'], dct['addTime'], dct['updateTime'])



# handleAppList group by author or executorAddress or executorHandler
def handle(applist):
    joblist = []
    for dct in applist:
        joblist.append(Job(dct['id'], dct['jobName'], dct['jobGroup'], dct['jobDesc'], dct['jobCron'], dct['executorParam'],
                           dct['executorAddress'], dct['executorHandler'], dct['author'], dct['jobStatus'], dct['addTime'], dct['updateTime']))
    # print(joblist)

    return joblist
    pass


# log list by jobName and jobGroup

# ping executorAddress

# show data


def remove(joblist):
    url = baseUrl + "/jobinfo/remove"
    print("joblist.size", len(joblist))
    for job in joblist:
        body = {
        "jobName": job.jobName,
        "jobGroup": job.jobGroup
        }
        resp = session.post(url, body)
        print(resp.status_code, resp.text)
    pass


def main():
    login("", "")
    applist = app_list();
    joblist = handle(applist)
    remove(joblist)
pass

if __name__ == '__main__':
  main()
